package com.ruicar.afs.cloud.product.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.product.entity.ProductPlanAtom;
import com.ruicar.afs.cloud.product.enums.ATOM_CONTROL_TYPE;
import com.ruicar.afs.cloud.product.mapper.ProductPlanAtomMapper;
import com.ruicar.afs.cloud.product.service.ProductPlanAtomService;
import com.ruicar.afs.cloud.product.vo.ProductPlanDataVo;
import com.ruicar.afs.cloud.product.vo.ProductPlanVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author changyu.gui
 * @version 1.0
 * @date 2020/5/14 18:06
 */
@Service
public class ProductPlanAtomServiceImpl extends ServiceImpl<ProductPlanAtomMapper, ProductPlanAtom> implements ProductPlanAtomService {
    /**
     * 保存或更新产品方案数据表
     *
     * @param object
     * @param id
     * @param type
     */
    public void saveOrUpdate(Object object, Long id, String type) {
        LinkedHashMap<String, Object> set = (LinkedHashMap) object;
        for (Map.Entry<String, Object> entry : set.entrySet()) {
            String mapKey = entry.getKey();
            String mapValue = "";
            if (StrUtil.isNotBlank(String.valueOf(entry.getValue())) && entry.getValue() != null) {
                mapValue = String.valueOf(entry.getValue());
            }
            if ("new".equals(type)) {
                ProductPlanAtom data = new ProductPlanAtom(id, mapKey, mapValue);
                this.save(data);
            } else {
                ProductPlanAtom data = this.getOne(Wrappers.<ProductPlanAtom>query().lambda().eq(ProductPlanAtom::getProductPlanId, id).eq(ProductPlanAtom::getProductAtomKey, mapKey), false);
                if (data != null && !mapValue.equals(data.getAtomValue())) {
                    data.setAtomValue(mapValue);
                    this.updateById(data);
                } else if (data == null) {
                    data = new ProductPlanAtom(id, mapKey, mapValue);
                    this.save(data);
                }
            }
        }
    }

    @Override
    public void copyProductPlanData(Long id, Long copyId) {
        List<ProductPlanAtom> list = new ArrayList<>();
        this.list(Wrappers.<ProductPlanAtom>query().lambda().eq(ProductPlanAtom::getProductPlanId, copyId)).stream().forEach(
                productPlanData -> {
                    productPlanData.setId(null);
                    productPlanData.setProductPlanId(id);
                    list.add(productPlanData);
                }
        );
        if (list.size() > 0) this.saveBatch(list);
    }

    public Map getProductPlanDataInfo(Long productPlanId, String atomShowType, String notAtomShowType) {
        List<ProductPlanVo> list = this.baseMapper.getProductPlanData(productPlanId, atomShowType, notAtomShowType);
        Map<String, Object> map = new HashMap<>();
        list.stream().forEach(
                productPlanVo -> {
                    Object value = null;
                    if (StrUtil.isNotBlank(productPlanVo.getValue())) {
                        String type = ATOM_CONTROL_TYPE.getType(productPlanVo.getType());
                        if (ATOM_CONTROL_TYPE.DECIMAL.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.SCALE.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.AMOUNT.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.DATE.getType().equals(type)) {
                            value = DateUtil.parse(productPlanVo.getValue(), "yyyy-MM-dd");
                        } else if (ATOM_CONTROL_TYPE.INTEGER.getType().equals(type)) {
                            value = Integer.valueOf(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.TIME.getType().equals(type)) {
                            value = DateUtil.parse(productPlanVo.getValue(), "HH:mm:ss");
                        } else if (ATOM_CONTROL_TYPE.CHECK_BOX.getType().equals(type) || ATOM_CONTROL_TYPE.MULTIPLE_SELECT.getType().equals(type)) {
                            String atomValue = productPlanVo.getValue().toString();
                            atomValue = atomValue.replace("[", "");
                            atomValue = atomValue.replace("]", "");
                            atomValue = atomValue.replace(" ", "");
                            value = atomValue.split(",");
                        } else {
                            value = productPlanVo.getValue();
                        }
                    }
                    map.put(productPlanVo.getKey(), value);
                }
        );
        return map;
    }

    /**
     * @param productID      产品ID
     * @param productAtomKey 原子key
     * @return
     * @Description 根据产品ID和原子key查询数据
     */
    public ProductPlanAtom getProductPlanData(Long productID, String productAtomKey) {
        ProductPlanAtom productPlanAtom = this.getOne(Wrappers.<ProductPlanAtom>query().lambda()
                        .eq(ProductPlanAtom::getProductAtomKey, productAtomKey)
                        .eq(ProductPlanAtom::getProductPlanId, productID)
                , false
        );
        return productPlanAtom;
    }

    @Override
    public List<ProductPlanDataVo> getProductPlanDataByProductId(Long productID, String atomShowType) {
        List<ProductPlanDataVo> list = this.baseMapper.getProductPlanDataByProductId(productID, atomShowType);
        return list;
    }

}
